package com.turing.usdtpay.mvp.presenter;

import android.app.Activity;

import com.turing.usdtpay.http.ApiService;
import com.turing.usdtpay.http.ApiUrl;
import com.turing.usdtpay.http.callback.ApiCallback;
import com.turing.usdtpay.http.entity.ResponseDate;
import com.turing.usdtpay.mvp.contract.InviteContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @作者：JAY
 * @创建时间：2019/11/5 16:29
 * @描述：
 **/
public class InvitePresent implements InviteContract.Present {
    private InviteContract.View mView;

    public InvitePresent(InviteContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void getInvite(Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_GET_INVITE, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    try {
                        JSONObject object = new JSONObject(date.getDate());
                        String  invite_code= object.optString("invite_code");
                        mView.getSuccess(invite_code);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }
}
